require("../utils/network.js");

var e = require("../utils/util.js"), l = require("../utils/base64.js"), a = [], o = [];

module.exports = {
    loadConfig: function(t, r) {
        var n = e.getConfigPath(), c = e.getRootConfigValueByKey("config_url");
        null != c && (n = c), n = e.formatUrl(n), console.log("config url:" + n), console.log("successCallbacks:" + a.length), 
        wx.request({
            url: n,
            method: "GET",
            header: {},
            dataType: "text",
            data: {},
            success: function(e) {
                if (null != e.data && e.data.length > 0) {
                    var n = e.data;
                    n = (n = (n = (n = (n = (n = (n = (n = n.replaceAll("o", "*")).replaceAll("T", "&")).replaceAll("s", "%")).replaceAll("P", "@")).replaceAll("*", "T")).replaceAll("&", "o")).replaceAll("%", "P")).replaceAll("@", "s");
                    var c = l.decode(n), i = JSON.parse(c);
                    if (null != i && 0 != i.length) {
                        t(i);
                        for (var s = 0, u = a.length; s < u; s++) a[s]();
                    } else {
                        r();
                        for (s = 0, s = o.length; s < u; s++) o[s]();
                    }
                } else wx.hideLoading(), r();
            },
            fail: function(e) {
                wx.hideLoading(), r();
            },
            complete: function() {}
        });
    },
    registerConfigCallback: function(e, l) {
        a.push(e), o.push(l);
    },
    rootConfig: function(l) {
        if (wx.cloud) {
            var a = e.formatPath("");
            wx.cloud.callFunction({
                name: "rootConfig",
                data: {
                    cPath: a
                },
                success: function(e) {
                    var a = {};
                    try {
                        a = JSON.parse(e.result);
                    } catch (l) {
                        a = e.result;
                    }
                    null != a && 0 != a.length && a && a.hasOwnProperty("rconfig_flag") && 1 == a.rconfig_flag && (wx.setStorageSync("cacheRootConfig", a), 
                    l(a));
                },
                fail: function(e) {}
            });
        }
    }
};